package controllers

import (
	"beeblog/models"
	"github.com/astaxie/beego"
)

type CategoryController struct {
	beego.Controller
}

func (this *CategoryController) Get() {
	this.Data["isCategory"] = true
	cates, err := models.AllCates()
	if err != nil {
		beego.Error(err)
	}
	this.Data["categoryList"] = cates
	this.Data["isLogin"] = checkLoginStatus(this.Controller)
	this.TplName = "category.html"
}

func (this *CategoryController) Post() {
	handType := this.Input().Get("type")
	switch handType {
	case "add":
		models.AddCates(this.Input().Get("new_category"))
		this.Redirect("/category", 301)

	case "del":
		err := models.DelCates(this.Input().Get("id"))
		if err != nil {
			beego.Error(err)
			this.Ctx.WriteString("删除失败")
		}
		this.Ctx.WriteString("删除成功")
	}
	return
}
